//
//  BrandCenterCtrl.m
//  cutter
//
//  Created by mac on 16/1/20.
//  Copyright © 2016年 e360. All rights reserved.
//
#import "BrandDetailCtrl.h"
#import "BrandCenterCtrl.h"
#import "ProductCenterCtrl.h"
#import "UserCenterPick.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>

@implementation BrandCenterCell
@end

@implementation BrandCenterCtrl
- (void)viewDidLoad {
    [super viewDidLoad];
    //
    __weak BrandCenterCtrl *this = self;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [this setData:[AppData brandList:this.condition]];
        dispatch_async(dispatch_get_main_queue(), ^{
            [this.collectionView reloadData];
        });
    });
}
- (NSMutableDictionary *)condition{
    if (_condition == nil) {
        [self setCondition:[NSMutableDictionary dictionary]];
        [self.condition setValue:@"" forKey:@"id"];
    }
    return _condition;
}
//
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.data count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *item = [self.data objectAtIndex:indexPath.item];
    if ([[item objectForKey:@"type"] intValue]==1) {
        BrandCenterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"playIdentifier" forIndexPath:indexPath];
        [AppData loadImage:[item objectForKey:@"cover"] view:cell.imageView];
        return cell;
    }
    BrandCenterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    [AppData loadImage:[item objectForKey:@"cover"] view:cell.imageView];
    return cell;
}

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
    if ([TMGlobal valueForKey:@"client"]) {
        if ([identifier isEqualToString:@"favorite"]) {
            [TMGlobal setValue:@(3) forKey:@"clientTag"];
            [self.tabBarController setSelectedIndex:8];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"UpdataUserCenter" object:nil];
            return NO;
        }
        if ([identifier isEqualToString:@"cart"]) {
            [TMGlobal setValue:@(4) forKey:@"clientTag"];
            [self.tabBarController setSelectedIndex:8];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"UpdataUserCenter" object:nil];
            return NO;
        }
        if ([identifier isEqualToString:@"client"]) {
            [self.tabBarController setSelectedIndex:8];
            return NO;
        }
        
    }
    return YES;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    __weak BrandCenterCtrl *this = self;
    if ([segue.destinationViewController isKindOfClass:UINavigationController.class]) {
        UINavigationController *ctrl = segue.destinationViewController;
        if ([[ctrl.viewControllers firstObject] isKindOfClass:UserCenterSelect.class]) {
            UserCenterSelect *root = [ctrl.viewControllers firstObject];
            [root setOnSelected:^(id target, id result){
                if(result){
                    if([segue.identifier isEqualToString:@"favorite"]){
                        [TMGlobal setValue:@(3) forKey:@"clientTag"];
                        [self.tabBarController setSelectedIndex:8];
                        [[NSNotificationCenter defaultCenter]postNotificationName:@"UpdataUserCenter" object:nil];
                    }
                    if([segue.identifier isEqualToString:@"cart"]){
                        [TMGlobal setValue:@(4) forKey:@"clientTag"];
                        [self.tabBarController setSelectedIndex:8];
                        [[NSNotificationCenter defaultCenter]postNotificationName:@"UpdataUserCenter" object:nil];
                    }
                }
                [this dismissViewControllerAnimated:YES completion:nil];
            }];
        }
    }
    if ([segue.destinationViewController isKindOfClass:BrandDetailCtrl.class]) {
        BrandDetailCtrl *ctrl = segue.destinationViewController;
        NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];
        if (indexPath) {
            NSDictionary *item = [self.data objectAtIndex:indexPath.item];
            [ctrl setData:[item objectForKey:@"album"]];
        }
    }
    if ([segue.destinationViewController isKindOfClass:AVPlayerViewController.class]) {
        AVPlayerViewController *ctrl = segue.destinationViewController;
        NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];
        if (indexPath) {
            NSDictionary *item = [self.data objectAtIndex:indexPath.item];
            NSString *video = [[[item objectForKey:@"video"] lastObject] objectForKey:@"videopath"];
            NSURL *mediaURL = [AppData brandVideo:video];
            if (mediaURL) {
                [ctrl setPlayer:[AVPlayer playerWithURL:mediaURL]];
                [ctrl.player play];
            }
        }
    }
}
-(IBAction)exitTouch:(id)sender {
    IndexHomeCtrl *tabbar = (id)self.splitViewController;
    if ([tabbar isKindOfClass:IndexHomeCtrl.class]) {
        [tabbar showMenu];
    }
}
@end